#include <fstream>
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <cstdlib>

using namespace std;

int dst(int month, int day, int hour, int month1, int day1, int month2, int day2){
  /*
    judge year,month,day,hour is daylight saving time or not
    month1,day1,2am in Spring start daylight saving time
    month2,day2,2am in Fall stop daylight saving time
   */
  if(month>month1&&month<month2) return 1;
  if(month==month1){
    if(day>day1) return 1;
    if(day==day1) if(hour>=2) return 1;
  }
  if(month==month2){
    if(day<day2) return 1;
    if(day==day2) if(hour<=1) return 1;
  }
  // default not dst
  return 0;
}

int isdst(int year, int month, int day, int hour) {
  /*
    Year Spring Forward  Fall Back
    2004 2 a.m. April 4  2 a.m. Oct. 31
    2005 2 a.m. April 3  2 a.m. Oct. 30
    2006 2 a.m. April 2  2 a.m. Oct. 29
    2007 2.a.m. March 11 2 a.m. Nov. 4
    2008 2 a.m. March 9  2 a.m. Nov. 2
    2009 2 a.m. March 8  2 a.m. Nov. 1
    2010 2 a.m. March 14 2 a.m. Nov 7
    2011 2 a.m. March 13 2 a.m. Nov. 6
  */
  if(year==2004) return dst(month, day, hour, 4, 4, 10, 31);
  if(year==2005) return dst(month, day, hour, 4, 3, 10, 30);
  if(year==2006) return dst(month, day, hour, 4, 2, 10, 29);
  if(year==2007) return dst(month, day, hour, 3, 11, 11, 4);
  if(year==2008) return dst(month, day, hour, 3, 9, 11, 2);
  if(year==2009) return dst(month, day, hour, 3, 8, 11, 1);
  if(year==2010) return dst(month, day, hour, 3, 14, 11, 7);
  if(year==2011) return dst(month, day, hour, 3, 13, 11, 6);
  // default not dst
  return 0;
}


int main(int argc, char **argv)
{
  if(argc!=2)
    {
      cout << "Usage: " << argv[0] << " DATAFILE" << endl;
      exit(1);
    }
  
  ifstream inputf1(argv[1]);
  string filename(argv[1]);
  if(filename.find(".dat")==string::npos) {
    cout << "Error: the data file name has to have the suffix .dat" << endl;
    exit(2);
  }
  filename.erase(filename.find(".dat"),4);
  filename+="_utc.dat";
  ofstream outputfile(filename.c_str());
  time_t rawtime;
  struct tm *timeinfo;
  string stime;
  int month, day, year, hour, minute, second;
  
  while(inputf1 >> stime) {
    sscanf(stime.c_str(),"%2d/%2d/%4d@%2d:%2d:%2d",&month,&day,&year,&hour,&minute,&second);
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    timeinfo->tm_year = year - 1900;
    timeinfo->tm_mon = month - 1;
    timeinfo->tm_mday = day;
    timeinfo->tm_hour = hour;
    timeinfo->tm_min = minute;
    timeinfo->tm_sec = second;
    timeinfo->tm_isdst = isdst(year, month, day, hour);
    
    time_t newtime=mktime ( timeinfo );
    tm* gmtm = gmtime(&newtime);
    char asout[256];
    sprintf (asout, "%02d/%02d/%4d@%02d:%02d:%02d", gmtm->tm_mon+1, gmtm->tm_mday, gmtm->tm_year+1900, gmtm->tm_hour, gmtm->tm_min, gmtm->tm_sec);
    outputfile << asout << endl;
  }  
  return 0;
  
}
